package com.sobjal.android.xml;

import java.io.StringWriter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.xmlpull.v1.XmlSerializer;

import com.sobjal.android.devices.Device;


import android.util.Xml;


/**
 * @author Fabio Monteiro
 * 
 * 	Defines functions to write xml for commands
 * 
 */
public  class XmlWriter {
	/**
	 * for sending a static choregraphy
	 * 
	 * @param idDevice
	 * @return
	 */
	public static String writeXmlForSendingAStaticChoregraphy(String idDevice){
		String s = 
			"<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
			+"<message>"
				+"<command name=\"chor\" />"
				+"<device id=\""+idDevice+"\" />"
				+"<parameters>"
					+"<tempo value=\"5\" />"
				+"</parameters>"
				+"<chor>"

			+"<ear time=\"0\" selectedEar=\"left\" rotAngle=\"60\" direction=\"front\" />"
			+"<ear time=\"5\" selectedEar=\"left\" rotAngle=\"40\" direction=\"back\" />"
			+"<ear time=\"5\" selectedEar=\"right\" rotAngle=\"40\" direction=\"front\" />"

			+"<ear time=\"10\" selectedEar=\"left\" rotAngle=\"300\" direction=\"front\" />"
			+"<ear time=\"10\" selectedEar=\"right\" rotAngle=\"120\" direction=\"back\" />"

			+"<led time=\"0\" selectedLed=\"top\" RColor=\"255\" GColor=\"0\" BColor=\"0\" />"
			+"<led time=\"1\" selectedLed=\"middle\" RColor=\"0\" GColor=\"255\" BColor=\"0\" />"
			+"<led time=\"2\" selectedLed=\"left\" RColor=\"0\" GColor=\"0\" BColor=\"255\" />"
			+"<led time=\"2\" selectedLed=\"right\" RColor=\"0\" GColor=\"0\" BColor=\"255\" />"

			+"<led time=\"3\" selectedLed=\"top\" RColor=\"0\" GColor=\"0\" BColor=\"0\" />"
			+"<led time=\"3\" selectedLed=\"middle\" RColor=\"0\" GColor=\"0\" BColor=\"0\" />"
			+"<led time=\"3\" selectedLed=\"left\" RColor=\"0\" GColor=\"0\" BColor=\"0\" />"
			+"<led time=\"3\" selectedLed=\"right\" RColor=\"0\" GColor=\"0\" BColor=\"0\" />"


			+"<led time=\"4\" selectedLed=\"left\" RColor=\"0\" GColor=\"0\" BColor=\"255\" />"
			+"<led time=\"4\" selectedLed=\"right\" RColor=\"0\" GColor=\"0\" BColor=\"255\" />"

			+"<led time=\"5\" selectedLed=\"middle\" RColor=\"0\" GColor=\"255\" BColor=\"0\" />"

			+"<led time=\"6\" selectedLed=\"top\" RColor=\"255\" GColor=\"0\" BColor=\"0\" />"
			+"<led time=\"7\" selectedLed=\"right\" RColor=\"0\" GColor=\"0\" BColor=\"0\" />"

			+"<ear time=\"3\" selectedEar=\"left\" rotAngle=\"300\" direction=\"front\" />"
			+"<ear time=\"3\" selectedEar=\"right\" rotAngle=\"300\" direction=\"front\" />"

			+"<!-- Reset K2000 -->"
			+"<led time=\"7\" selectedLed=\"middle\" RColor=\"0\" GColor=\"0\" BColor=\"0\" />"
			+"<led time=\"7\" selectedLed=\"left\" RColor=\"0\" GColor=\"0\" BColor=\"0\" />"
			+"<led time=\"7\" selectedLed=\"right\" RColor=\"0\" GColor=\"0\" BColor=\"0\" />"

			+"<!-- Start left K2000 -->"
			+"<led time=\"8\" selectedLed=\"left\" RColor=\"255\" GColor=\"0\" BColor=\"0\" />"
			+"<led time=\"9\" selectedLed=\"middle\" RColor=\"255\" GColor=\"0\" BColor=\"0\" />"
			+"<led time=\"10\" selectedLed=\"right\" RColor=\"255\" GColor=\"0\" BColor=\"0\" />"

			+"<!-- Reset K2000 -->"
			+"<led time=\"11\" selectedLed=\"middle\" RColor=\"0\" GColor=\"0\" BColor=\"0\" />"
			+"<led time=\"11\" selectedLed=\"left\" RColor=\"0\" GColor=\"0\" BColor=\"0\" />"
			+"<led time=\"11\" selectedLed=\"right\" RColor=\"0\" GColor=\"0\" BColor=\"0\" />"

			+"<!-- Start right K2000 -->"
			+"<led time=\"11\" selectedLed=\"right\" RColor=\"255\" GColor=\"0\" BColor=\"0\" />"
			+"<led time=\"12\" selectedLed=\"middle\" RColor=\"255\" GColor=\"0\" BColor=\"0\" />"
			+"<led time=\"13\" selectedLed=\"left\" RColor=\"255\" GColor=\"0\" BColor=\"0\" />"

			+"<!-- Reset K2000 -->"
			+"<led time=\"14\" selectedLed=\"middle\" RColor=\"0\" GColor=\"0\" BColor=\"0\" />"
			+"<led time=\"14\" selectedLed=\"left\" RColor=\"0\" GColor=\"0\" BColor=\"0\" />"
			+"<led time=\"14\" selectedLed=\"right\" RColor=\"0\" GColor=\"0\" BColor=\"0\" />"

			+"<!-- Start left K2000 -->"
			+"<led time=\"14\" selectedLed=\"left\" RColor=\"255\" GColor=\"0\" BColor=\"0\" />"
			+"<led time=\"15\" selectedLed=\"middle\" RColor=\"255\" GColor=\"0\" BColor=\"0\" />"
			+"<led time=\"16\" selectedLed=\"right\" RColor=\"255\" GColor=\"0\" BColor=\"0\" />"

			+"<!-- Reset K2000 -->"
			+"<led time=\"17\" selectedLed=\"middle\" RColor=\"0\" GColor=\"0\" BColor=\"0\" />"
			+"<led time=\"17\" selectedLed=\"left\" RColor=\"0\" GColor=\"0\" BColor=\"0\" />"
			+"<led time=\"17\" selectedLed=\"right\" RColor=\"0\" GColor=\"0\" BColor=\"0\" />"

			+"<!-- Start right K2000 -->"
			+"<led time=\"17\" selectedLed=\"right\" RColor=\"255\" GColor=\"0\" BColor=\"0\" />"
			+"<led time=\"18\" selectedLed=\"middle\" RColor=\"255\" GColor=\"0\" BColor=\"0\" />"
			+"<led time=\"19\" selectedLed=\"left\" RColor=\"255\" GColor=\"0\" BColor=\"0\" />"

			+"<!-- Reset K2000 -->"
			+"<led time=\"20\" selectedLed=\"middle\" RColor=\"0\" GColor=\"0\" BColor=\"0\" />"
			+"<led time=\"20\" selectedLed=\"left\" RColor=\"0\" GColor=\"0\" BColor=\"0\" />"
			+"<led time=\"20\" selectedLed=\"right\" RColor=\"0\" GColor=\"0\" BColor=\"0\" />"

			+"<!-- Start left K2000 -->"
			+"<led time=\"20\" selectedLed=\"left\" RColor=\"102\" GColor=\"255\" BColor=\"255\" />"
			+"<led time=\"21\" selectedLed=\"middle\" RColor=\"102\" GColor=\"255\" BColor=\"255\" />"
			+"<led time=\"22\" selectedLed=\"right\" RColor=\"102\" GColor=\"255\" BColor=\"255\"  />"

			+"<!-- Reset K2000 -->"
			+"<led time=\"23\" selectedLed=\"middle\" RColor=\"0\" GColor=\"0\" BColor=\"0\" />"
			+"<led time=\"23\" selectedLed=\"left\" RColor=\"0\" GColor=\"0\" BColor=\"0\" />"
			+"<led time=\"23\" selectedLed=\"right\" RColor=\"0\" GColor=\"0\" BColor=\"0\" />"

			+"<!-- Start right K2000 -->"
			+"<led time=\"23\" selectedLed=\"right\" RColor=\"102\" GColor=\"255\" BColor=\"255\" />"
			+"<led time=\"24\" selectedLed=\"middle\" RColor=\"102\" GColor=\"255\" BColor=\"255\" />"
			+"<led time=\"25\" selectedLed=\"left\" RColor=\"102\" GColor=\"255\" BColor=\"255\" />"
			+"<!-- K2000 end -->"

			+"<led time=\"39\" selectedLed=\"top\" RColor=\"0\" GColor=\"0\" BColor=\"0\" />"
			+"<led time=\"39\" selectedLed=\"middle\" RColor=\"0\" GColor=\"0\" BColor=\"0\" />"
			+"<led time=\"39\" selectedLed=\"left\" RColor=\"0\" GColor=\"0\" BColor=\"0\" />"
			+"<led time=\"39\" selectedLed=\"right\" RColor=\"0\" GColor=\"0\" BColor=\"0\" />"

			+"<!-- Start triangle -->"
			+"<led time=\"40\" selectedLed=\"top\" RColor=\"102\" GColor=\"255\" BColor=\"255\" />"
			+"<led time=\"40\" selectedLed=\"left\" RColor=\"102\" GColor=\"255\" BColor=\"255\" />"
			+"<led time=\"40\" selectedLed=\"right\" RColor=\"102\" GColor=\"255\" BColor=\"255\" />"

			+"<!-- Reset triangle -->"
			+"<led time=\"41\" selectedLed=\"top\" RColor=\"0\" GColor=\"0\" BColor=\"0\" />"
			+"<led time=\"41\" selectedLed=\"left\" RColor=\"0\" GColor=\"0\" BColor=\"0\" />"
			+"<led time=\"41\" selectedLed=\"right\" RColor=\"0\" GColor=\"0\" BColor=\"0\" />"

			+"<!-- Display triangle -->"
			+"<led time=\"42\" selectedLed=\"top\" RColor=\"102\" GColor=\"255\" BColor=\"255\" />"
			+"<led time=\"43\" selectedLed=\"left\" RColor=\"102\" GColor=\"255\" BColor=\"255\" />"
			+"<led time=\"44\" selectedLed=\"right\" RColor=\"102\" GColor=\"255\" BColor=\"255\" />"

			+"<!-- Reset triangle -->"
			+"<led time=\"41\" selectedLed=\"top\" RColor=\"0\" GColor=\"0\" BColor=\"0\" />"
			+"<led time=\"41\" selectedLed=\"left\" RColor=\"0\" GColor=\"0\" BColor=\"0\" />"
			+"<led time=\"41\" selectedLed=\"right\" RColor=\"0\" GColor=\"0\" BColor=\"0\" />"

			+"<!-- Display triangle -->"
			+"<led time=\"42\" selectedLed=\"top\" RColor=\"102\" GColor=\"255\" BColor=\"255\" />"
			+"<led time=\"43\" selectedLed=\"left\" RColor=\"102\" GColor=\"255\" BColor=\"255\" />"
			+"<led time=\"44\" selectedLed=\"right\" RColor=\"102\" GColor=\"255\" BColor=\"255\" />"

			  
			+"<led time=\"45\" selectedLed=\"top\" RColor=\"255\" GColor=\"0\" BColor=\"0\" />"
			+"<led time=\"45\" selectedLed=\"left\" RColor=\"102\" GColor=\"255\" BColor=\"255\" />"
			+"<led time=\"45\" selectedLed=\"right\" RColor=\"102\" GColor=\"255\" BColor=\"255\" />"

			 
			+"<led time=\"46\" selectedLed=\"top\" RColor=\"102\" GColor=\"255\" BColor=\"255\" />"
			+"<led time=\"46\" selectedLed=\"left\" RColor=\"255\" GColor=\"0\" BColor=\"0\" />"
			+"<led time=\"46\" selectedLed=\"right\" RColor=\"102\" GColor=\"255\" BColor=\"255\" />"

			 
			+"<led time=\"47\" selectedLed=\"top\" RColor=\"102\" GColor=\"255\" BColor=\"255\" />"
			+"<led time=\"47\" selectedLed=\"left\" RColor=\"102\" GColor=\"255\" BColor=\"255\"  />"
			+"<led time=\"47\" selectedLed=\"right\" RColor=\"255\" GColor=\"0\" BColor=\"0\" />"

			  
			+"<led time=\"48\" selectedLed=\"top\" RColor=\"255\" GColor=\"0\" BColor=\"0\" />"
			+"<led time=\"48\" selectedLed=\"left\" RColor=\"102\" GColor=\"255\" BColor=\"255\" />"
			+"<led time=\"48\" selectedLed=\"right\" RColor=\"102\" GColor=\"255\" BColor=\"255\" />"

			 
			+"<led time=\"49\" selectedLed=\"top\" RColor=\"102\" GColor=\"255\" BColor=\"255\" />"
			+"<led time=\"49\" selectedLed=\"left\" RColor=\"255\" GColor=\"0\" BColor=\"0\" />"
			+"<led time=\"49\" selectedLed=\"right\" RColor=\"102\" GColor=\"255\" BColor=\"255\" />"

			 
			+"<led time=\"50\" selectedLed=\"top\" RColor=\"102\" GColor=\"255\" BColor=\"255\" />"
			+"<led time=\"50\" selectedLed=\"left\" RColor=\"102\" GColor=\"255\" BColor=\"255\"  />"
			+"<led time=\"50\" selectedLed=\"right\" RColor=\"255\" GColor=\"0\" BColor=\"0\" />"

			  
			+"<led time=\"51\" selectedLed=\"top\" RColor=\"255\" GColor=\"0\" BColor=\"0\" />"
			+"<led time=\"51\" selectedLed=\"left\" RColor=\"102\" GColor=\"255\" BColor=\"255\" />"
			+"<led time=\"51\" selectedLed=\"right\" RColor=\"102\" GColor=\"255\" BColor=\"255\" />"

			 
			+"<led time=\"52\" selectedLed=\"top\" RColor=\"102\" GColor=\"255\" BColor=\"255\" />"
			+"<led time=\"52\" selectedLed=\"left\" RColor=\"255\" GColor=\"0\" BColor=\"0\" />"
			+"<led time=\"52\" selectedLed=\"right\" RColor=\"102\" GColor=\"255\" BColor=\"255\" />"

			 
			+"<led time=\"53\" selectedLed=\"top\" RColor=\"102\" GColor=\"255\" BColor=\"255\" />"
			+"<led time=\"53\" selectedLed=\"left\" RColor=\"102\" GColor=\"255\" BColor=\"255\"  />"
			+"<led time=\"53\" selectedLed=\"right\" RColor=\"255\" GColor=\"0\" BColor=\"0\" />"


			  
			+"<led time=\"54\" selectedLed=\"top\" RColor=\"255\" GColor=\"0\" BColor=\"0\" />"
			+"<led time=\"54\" selectedLed=\"left\" RColor=\"102\" GColor=\"255\" BColor=\"255\" />"
			+"<led time=\"54\" selectedLed=\"right\" RColor=\"102\" GColor=\"255\" BColor=\"255\" />"

			 
			+"<led time=\"55\" selectedLed=\"top\" RColor=\"102\" GColor=\"255\" BColor=\"255\" />"
			+"<led time=\"55\" selectedLed=\"left\" RColor=\"255\" GColor=\"0\" BColor=\"0\" />"
			+"<led time=\"55\" selectedLed=\"right\" RColor=\"102\" GColor=\"255\" BColor=\"255\" />"

			 
			+"<led time=\"56\" selectedLed=\"top\" RColor=\"102\" GColor=\"255\" BColor=\"255\" />"
			+"<led time=\"56\" selectedLed=\"left\" RColor=\"102\" GColor=\"255\" BColor=\"255\"  />"
			+"<led time=\"56\" selectedLed=\"right\" RColor=\"255\" GColor=\"0\" BColor=\"0\" />"

			+"<!-- Bling bling -->"
			+"<led time=\"57\" selectedLed=\"top\" RColor=\"0\" GColor=\"0\" BColor=\"0\" />"
			+"<led time=\"57\" selectedLed=\"left\" RColor=\"0\" GColor=\"0\" BColor=\"0\" />"
			+"<led time=\"57\" selectedLed=\"right\" RColor=\"0\" GColor=\"0\" BColor=\"255\" />"

			+"<led time=\"58\" selectedLed=\"top\" RColor=\"102\" GColor=\"255\" BColor=\"255\" />"
			+"<led time=\"58\" selectedLed=\"left\" RColor=\"102\" GColor=\"255\" BColor=\"255\" />"
			+"<led time=\"58\" selectedLed=\"right\" RColor=\"102\" GColor=\"255\" BColor=\"255\" />"

			+"<led time=\"59\" selectedLed=\"top\" RColor=\"102\" GColor=\"255\" BColor=\"255\" />"
			+"<led time=\"59\" selectedLed=\"left\" RColor=\"102\" GColor=\"255\" BColor=\"255\" />"
			+"<led time=\"59\" selectedLed=\"right\" RColor=\"102\" GColor=\"255\" BColor=\"255\" />"

			+"<led time=\"60\" selectedLed=\"top\" RColor=\"0\" GColor=\"0\" BColor=\"0\" />"
			+"<led time=\"60\" selectedLed=\"left\" RColor=\"0\" GColor=\"0\" BColor=\"0\" />"
			+"<led time=\"60\" selectedLed=\"right\" RColor=\"0\" GColor=\"0\" BColor=\"255\" />"

			+"<led time=\"61\" selectedLed=\"top\" RColor=\"102\" GColor=\"255\" BColor=\"255\" />"
			+"<led time=\"61\" selectedLed=\"left\" RColor=\"102\" GColor=\"255\" BColor=\"255\" />"
			+"<led time=\"61\" selectedLed=\"right\" RColor=\"102\" GColor=\"255\" BColor=\"255\" />"

			+"<led time=\"62\" selectedLed=\"top\" RColor=\"102\" GColor=\"255\" BColor=\"255\" />"
			+"<led time=\"62\" selectedLed=\"left\" RColor=\"102\" GColor=\"255\" BColor=\"255\" />"
			+"<led time=\"62\" selectedLed=\"right\" RColor=\"102\" GColor=\"255\" BColor=\"255\" />"

			+"<led time=\"65\" selectedLed=\"top\" RColor=\"0\" GColor=\"0\" BColor=\"0\" />"
			+"<led time=\"64\" selectedLed=\"left\" RColor=\"0\" GColor=\"0\" BColor=\"0\" />"
			+"<led time=\"63\" selectedLed=\"right\" RColor=\"0\" GColor=\"0\" BColor=\"0\"/>"

			+"<led time=\"66\" selectedLed=\"top\" RColor=\"102\" GColor=\"255\" BColor=\"255\" />"
			+"<led time=\"66\" selectedLed=\"left\" RColor=\"102\" GColor=\"255\" BColor=\"255\" />"
			+"<led time=\"66\" selectedLed=\"right\" RColor=\"102\" GColor=\"255\" BColor=\"255\" />"

			+"<led time=\"67\" selectedLed=\"top\" RColor=\"102\" GColor=\"255\" BColor=\"255\" />"
			+"<led time=\"67\" selectedLed=\"left\" RColor=\"102\" GColor=\"255\" BColor=\"255\" />"
			+"<led time=\"67\" selectedLed=\"right\" RColor=\"102\" GColor=\"255\" BColor=\"255\" />"

			+"<ear time=\"50\" selectedEar=\"left\" rotAngle=\"60\" direction=\"front\" />"
			+"<ear time=\"55\" selectedEar=\"left\" rotAngle=\"40\" direction=\"back\" />"
			+"<ear time=\"55\" selectedEar=\"right\" rotAngle=\"40\" direction=\"front\" />"

			+"<ear time=\"60\" selectedEar=\"left\" rotAngle=\"100\" direction=\"front\" />"
			+"<ear time=\"60\" selectedEar=\"right\" rotAngle=\"120\" direction=\"back\" />"

				+"</chor>"
			+"</message>";
		
		return s;
	}
	
	
	/**
	 * for sending a customised by user choregrpahy (Nabaz)
	 * 
	 * @param earLed
	 * @param idDevice
	 * @param tempo
	 * @return
	 */
	public static String writeXmlForSendingACustomChoregraphy(ArrayList<ArrayList<String>> earLed, String idDevice, String tempo )
	{
		XmlSerializer s = Xml.newSerializer();
		StringWriter w = new StringWriter();
		
		try{
			s.setOutput(w);
			s.startDocument("UTF-8", true);
			s.startTag("", "message");
				s.startTag("", "command");
				s.attribute("", "name", "chor");
				s.endTag("", "command");
				
				s.startTag("", "device");
				s.attribute("","id", idDevice);
				s.endTag("", "device");
				
				s.startTag("", "parameters");
					s.startTag("", "tempo");
					s.text(tempo);
					s.endTag("", "tempo");
				s.endTag("", "parameters");
				
				s.startTag("", "chor");
					Iterator<ArrayList<String>> i = earLed.iterator();
					
					while(i.hasNext()){
						ArrayList<String> temp = i.next();
						
						if(temp.get(0).equalsIgnoreCase("ear")){
							s.startTag("", "ear");
								s.attribute("", "time", temp.get(1));
								s.attribute("", "selectedEar", temp.get(2));
								s.attribute("", "rotAngle", temp.get(3));
								s.attribute("", "direction", temp.get(4));
							s.endTag("", "ear");
						}
						
						if(temp.get(0).equalsIgnoreCase("led")){
							s.startTag("", "led");
								s.attribute("", "time", temp.get(1));
								s.attribute("", "selectedLed", temp.get(2));
								s.attribute("", "RColor", temp.get(3));
								s.attribute("", "GColor", temp.get(4));
								s.attribute("", "BColor", temp.get(5));
							s.endTag("", "led");
						}
					}
				s.endTag("", "chor");		
			s.endTag("", "message");
			s.endDocument();
			return w.toString();
		}catch (Exception e) {
	        throw new RuntimeException(e);
	    }
	}
	
	/**
	 * for playing a link/song with an URL(Nabaz)
	 * 
	 * @param idDevice
	 * @param link
	 * @return
	 */
	public static String writeXmlForPlayURL(String idDevice, String link){
		XmlSerializer s = Xml.newSerializer();
		StringWriter w = new StringWriter();
		
		try{
			s.setOutput(w);
			s.startDocument("UTF-8", true);
			s.startTag("", "message");
				s.startTag("", "command");
				s.attribute("", "name", "play");
				s.endTag("", "command");
				
				s.startTag("", "device");
				s.attribute("","id", idDevice);
				s.endTag("", "device");
				
					s.startTag("", "parameters");
						s.startTag("", "mp3Link");
						s.attribute("", "value", link);
						s.endTag("", "mp3Link");
					s.endTag("", "parameters");
			s.endTag("", "message");
			s.endDocument();
			return w.toString();
		}catch (Exception e) {
	        throw new RuntimeException(e);
	    }
	}
	
	/**
	 * wakeUp (Nabaz)
	 * 
	 * @param idDevice
	 * @return
	 */
	public static String writeXmlForWakeUp(String idDevice){
		XmlSerializer s = Xml.newSerializer();
		StringWriter w = new StringWriter();
		
		try{
			s.setOutput(w);
			s.startDocument("UTF-8", true);
			s.startTag("", "message");
				s.startTag("", "command");
				s.attribute("", "name", "wakeUp");
				s.endTag("", "command");
				
				s.startTag("", "device");
				s.attribute("","id", idDevice);
				s.endTag("", "device");
				
				s.startTag("", "parameters");
				s.endTag("", "parameters");
			s.endTag("", "message");
			s.endDocument();
			return w.toString();
		}catch (Exception e) {
	        throw new RuntimeException(e);
	    }
	}
	
	/**
	 * make sleep (Nabaz)
	 * 
	 * @param idDevice
	 * @return
	 */
	public static String writeXmlForMakeSleep(String idDevice){
		XmlSerializer s = Xml.newSerializer();
		StringWriter w = new StringWriter();
		
		try{
			s.setOutput(w);
			s.startDocument("UTF-8", true);
			s.startTag("", "message");
				s.startTag("", "command");
				s.attribute("", "name", "sleep");
				s.endTag("", "command");
				
				s.startTag("", "device");
				s.attribute("","id", idDevice);
				s.endTag("", "device");
				
				s.startTag("", "parameters");
				s.endTag("", "parameters");
			s.endTag("", "message");
			s.endDocument();
			return w.toString();
		}catch (Exception e) {
	        throw new RuntimeException(e);
	    }
	}
	
	
	
	/**
	 * is sleeping ? (Nabaz)
	 * 
	 * @param idDevice
	 * @return
	 */
	public static String writeXmlForIsSleeping( String idDevice){
		XmlSerializer s = Xml.newSerializer();
		StringWriter w = new StringWriter();
		
		try{
			s.setOutput(w);
			s.startDocument("UTF-8", true);
			s.startTag("", "message");
				s.startTag("", "command");
				s.attribute("", "name", "isSleeping");
				s.endTag("", "command");
				
				s.startTag("", "device");
				s.attribute("","id", idDevice);
				s.endTag("", "device");
				
				s.startTag("", "parameters");
				s.endTag("", "parameters");
			s.endTag("", "message");
			s.endDocument();
			return w.toString();
		}catch (Exception e) {
	        throw new RuntimeException(e);
	    }
	}
	
	/**
	 * reset ears (Nabaz)
	 * 
	 * @param idDevice
	 * @return
	 */
	public static String writeXmlForResettingEars(String idDevice){
		XmlSerializer s = Xml.newSerializer();
		StringWriter w = new StringWriter();
		
		try{
			s.setOutput(w);
			s.startDocument("UTF-8", true);
			s.startTag("", "message");
				s.startTag("", "command");
				s.attribute("", "name", "resetEar");
				s.endTag("", "command");
				
				s.startTag("", "device");
				s.attribute("","id", idDevice);
				s.endTag("", "device");
				
				s.startTag("", "parameters");
				s.endTag("", "parameters");
			s.endTag("", "message");
			s.endDocument();
			return w.toString();
		}catch (Exception e) {
	        throw new RuntimeException(e);
	    }
	}
	
	/**
	 * choregraphy with tempo specified by user (Nabaz)
	 * 
	 * @param idDevice
	 * @param tempo
	 * @return
	 */
	public static String writeXmlForCommandChoregraphyWithTempo(String idDevice, String tempo){
		XmlSerializer s = Xml.newSerializer();
		StringWriter w = new StringWriter();
		
		try{
			s.setOutput(w);
			s.startDocument("UTF-8", true);
			s.startTag("", "message");
				s.startTag("", "command");
				s.attribute("", "name", "chor"); //'chor' here
				s.endTag("", "command");
				
				s.startTag("", "device");
				s.attribute("","id", idDevice);
				s.endTag("", "device");
				
				s.startTag("", "parameters");
					s.startTag("","tempo");
					s.endTag("","tempo");
				s.endTag("", "parameters");
				
				s.startTag("", "chor");
					s.startTag("","ear");
						s.attribute("", "time", "");
						s.attribute("", "selectedEar", "");
						s.attribute("", "rotAngle", "");
						s.attribute("", "direction", "");
					s.endTag("","ear");
						
					s.startTag("","led");
						s.attribute("", "time", "");
						s.attribute("", "selectedEar", "");
						s.attribute("", "RColor", "");
						s.attribute("", "GColor", "");
						s.attribute("", "BColor", "");
					s.endTag("","led");	
				s.endTag("", "chor");	
				
			s.endTag("", "message");
			s.endDocument();
			return w.toString();
		}catch (Exception e) {
	        throw new RuntimeException(e);
	    }
	}

	/**
	 * to get all friends (Nabaz)
	 * 
	 * @param idDevice
	 * @return
	 */
	public static String writeXmlForGettingFriends(String idDevice){
		XmlSerializer s = Xml.newSerializer();
		StringWriter w = new StringWriter();
		
		try{
			s.setOutput(w);
			s.startDocument("UTF-8", true);
			s.startTag("", "message");
				s.startTag("", "command");
				s.attribute("", "name", "getFriends");
				s.endTag("", "command");
				
				s.startTag("", "device");
				s.attribute("","id", idDevice);
				s.endTag("", "device");
				
				s.startTag("", "parameters");
				s.endTag("", "parameters");
			s.endTag("", "message");
			s.endDocument();
			return w.toString();
		}catch (Exception e) {
	        throw new RuntimeException(e);
	    }
	}
	
	/**
	 * for sending a message (Nabaz, mydeskfriend)
	 * 
	 * @param idDevice
	 * @param message
	 * @return
	 */
	public static String writeXmlForSendingMessage(String idDevice, String message){	
		XmlSerializer s = Xml.newSerializer();
		StringWriter w = new StringWriter();
		
		try{
			s.setOutput(w);
			s.startDocument("UTF-8", true);
			s.startTag("", "message");
				s.startTag("", "command");
				s.attribute("", "name", "sendText");
				s.endTag("", "command");
				
				s.startTag("", "device");
				s.attribute("","id", idDevice);
				s.endTag("", "device");
				
				s.startTag("", "parameters");
					s.startTag("", "text");
					s.attribute("", "value", message);
					s.endTag("", "text");
				s.endTag("", "parameters");
			s.endTag("", "message");
			s.endDocument();
			return w.toString();
		}catch (Exception e) {
	        throw new RuntimeException(e);
	    }
	}
	
	/**
	 * For login
	 * 
	 * @param login
	 * @param password
	 * @return
	 */
	public static String writeXmlForLogin(String login, String password){	//12.01.2010
		XmlSerializer s = Xml.newSerializer();
		StringWriter w = new StringWriter();

		try{
			s.setOutput(w);
			s.startDocument("UTF-8", true);
			s.startTag("", "message");
				s.startTag("", "command");
				s.attribute("", "name", "login");
				s.endTag("", "command");
				s.startTag("", "parameters");
					s.startTag("", "loginName");
						s.attribute("", "value", login);
					s.endTag("", "loginName");
					s.startTag("","password");
						s.attribute("", "value", password);
					s.endTag("", "password");
				s.endTag("", "parameters");
			s.endTag("", "message");
			s.endDocument();
			return w.toString();
		}catch (Exception e) {
	        throw new RuntimeException(e);
	    }
	}
	
	
	
	/**
	 * Command whitout any parameters but its name
	 * 
	 * @param idDevice
	 * @param nameCommand
	 * @return
	 */
	public static String writeXmlForCommandWhtitoutParameters (String idDevice, String nameCommand){
		XmlSerializer s = Xml.newSerializer();
		StringWriter w = new StringWriter();
		
		try{
			s.setOutput(w);
			s.startDocument("UTF-8", true);
			s.startTag("", "message");
				s.startTag("", "command");
				s.attribute("", "name", nameCommand);
				s.endTag("", "command");
				
				s.startTag("", "device");
				s.attribute("","id", idDevice);
				s.endTag("", "device");
				
				s.startTag("", "parameters");
				s.endTag("", "parameters");
			s.endTag("", "message");
			s.endDocument();
			return w.toString();
		}catch (Exception e) {
	        throw new RuntimeException(e);
	    }
	}
		

	
	//OLD
	public static String writeXmlForLogin_old(String login, String password){	//first version
		XmlSerializer s = Xml.newSerializer();
		StringWriter w = new StringWriter();

		try{
			s.setOutput(w);
			s.startDocument("UTF-8", true);
			s.startTag("", "message");
				s.startTag("", "login");
					s.startTag("", "LoginName");
					s.attribute("", "value", login);
					s.endTag("", "LoginName");
					s.startTag("","pwd");
					s.attribute("", "value", password);
					s.endTag("", "pwd");
				s.endTag("", "login");
			s.endTag("", "message");
			s.endDocument();
			return w.toString();
		}catch (Exception e) {
	        throw new RuntimeException(e);
	    }
	}
	

}
